import java.util.*;
import java.util.stream.Collectors;

/**
 * @author zhouyanxiang
 * @Date 2020-11-2020/11/23-11:21
 */
public class Main {

    public static void main(String[] args) {

        // 生成流
        List<String> strings = Arrays.asList("Tom","Jerry","Mike","Tony","");
        List<String> filtered = new ArrayList<>(strings);
        for (String str : filtered) {
            System.out.print(str + "  ");
        }

        System.out.println();
        System.out.println("==============");
        // forEach
        // limit
        Random random = new Random();
        random.ints().limit(10).forEach(System.out::println);

        System.out.println("==============");
        // map
        List<Integer> numbers = Arrays.asList(1,2,3,4,5,6);
        List<Integer> subList = numbers.stream().map(i -> i*i).collect(Collectors.toList());
        for (Integer integer : subList) {
            System.out.print(integer + " ");
        }

        System.out.println();
        System.out.println("==============");
        //
        List<String> str = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        // 获取空字符串的数量
        long count = str.stream().filter(String::isEmpty).count();
        System.out.println(count);

        System.out.println("==============");
        // sorted
        Random r = new Random();
        r.ints().limit(10).sorted().forEach(System.out::println);

        System.out.println("==============");
        // parallel
        List<String> strNum = Arrays.asList("abc", "","","","bc", "efg", "abcd","", "jkl");
        // 获取空字符串的数量
        long num = strNum.parallelStream().filter(String::isEmpty).count();
        System.out.println(num);

        System.out.println("==============");
        // Collectors
        List<String> stringNum = Arrays.asList("abc", "", "bc", "efg", "abcd","", "jkl");
        List<String> filter = stringNum.stream().filter(string -> !string.isEmpty()).collect(Collectors.toList());

        System.out.println("筛选列表: " + filter);
        String mergedString = stringNum.stream().filter(string -> !string.isEmpty()).collect(Collectors.joining(", "));
        System.out.println("合并字符串: " + mergedString);

        // 统计
        List<Integer> numberCount = Arrays.asList(3, 2, 2, 3, 7, 3, 5);
        IntSummaryStatistics stats = numberCount.stream().mapToInt((x) -> x).summaryStatistics();

        System.out.println("列表中最大的数 : " + stats.getMax());
        System.out.println("列表中最小的数 : " + stats.getMin());
        System.out.println("所有数之和 : " + stats.getSum());
        System.out.println("平均数 : " + stats.getAverage());
    }

}
